<template>
  <div>
    <el-form label-width="100px">
      <el-form-item label="上级考点">
        {{parentName}}
      </el-form-item>
      <el-form-item label="考点名称">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="form.description" type="textarea" :rows="4"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="submit" type="primary">保存并返回</el-button>
      </el-form-item>
    </el-form>

    <!-- 柱状图 -->
    <div id="echarts-box"></div>
    <div id="abc">{{count}}</div>
  </div>
</template>

<style scoped>
#echarts-box {
  width: 500px;
  height: 500px;
}
</style>

<script>
import axios from 'axios'
import * as echarts from 'echarts'
export default {
  data() {
    return {
      count: 1,
      parentName: this.$route.query.parentName || '顶级分类', // 上级分类名称
      form: {
        id: this.$route.query.id || 0, // 要修改的记录的ID，0：添加,非0代表是修改
        parent_id: this.$route.query.parent_id || 0, // 如果没传就是0，否则就用传的值
        name: '',
        description: ''
      }
    }
  },
  created() {
    // 判断如果是修改，需要使用 id 获取这条记录的详细信息填充到表单中
    if (this.form.id > 0) {
      axios.get('/exam/point/' + this.form.id).then(res => {
        if (res) {
          // 把数据填充到表单中
          this.form.name = res.data.name
          this.form.description = res.data.description
        }
      })
    }

    // 注册一个函数，这个函数会在页面下次更新完之后执行~~~
    this.$nextTick(() => {
      var chartDom = document.getElementById('echarts-box')
      var myChart = echarts.init(chartDom)
      var option

      option = {
        tooltip: {
          trigger: 'item'
        },
        legend: {
          top: '5%',
          left: 'center'
        },
        series: [
          {
            name: '访问来源',
            type: 'pie',
            radius: ['40%', '70%'],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: '#fff',
              borderWidth: 2
            },
            label: {
              show: false,
              position: 'center'
            },
            emphasis: {
              label: {
                show: true,
                fontSize: '40',
                fontWeight: 'bold'
              }
            },
            labelLine: {
              show: false
            },
            data: [
              { value: 1048, name: '搜索引擎' },
              { value: 735, name: '直接访问' },
              { value: 580, name: '邮件营销' },
              { value: 484, name: '联盟广告' },
              { value: 300, name: '视频广告' }
            ]
          }
        ]
      }

      option && myChart.setOption(option)
    })
  },
  // 组件挂载到页面之后执行
  mounted() {
    this.count = 2
    // 更新完之后，直接从页面中获取，得到的还是原来的数据
    // 因为是延迟更新，代码执行完之后才会更新
    // console.log(document.getElementById('abc').innerHTML)
    // 如果非要取，就得在页面更新完之后再取
    this.$nextTick(() => {
      console.log(document.getElementById('abc').innerHTML)
    })
  },
  methods: {
    submit() {
      if (this.form.id > 0) {
        // 调用修改
        axios.put('/exam/point/' + this.form.id, this.form).then(res => {
          if (res) {
            this.$message.success('修改成功')
            this.$router.push({ name: 'QuestionQuestionCheckPointManage' })
          }
        })
      } else {
        // 添加接口
        axios.post('/exam/point', this.form).then(res => {
          if (res) {
            this.$message.success('添加成功')
            this.$router.push({ name: 'QuestionQuestionCheckPointManage' })
          }
        })
      }
    }
  }
}
</script>
